package jsonTest;

import java.io.IOException;

import com.ruiyi.error.RuiyiCredentialsException;
import com.ruiyi.error.RuiyiError;
import com.ruiyi.error.RuiyiException;

import handler.Handler;
import handler.Message;
import json.Json_Account;
import json.Json_Company;

import util.LogUtil;

public class MyCompany extends BaseCompany{
	protected static final String TAG = "MyCompany";
	Json_Account account;
	
	public void setAccount(Json_Account account){
		this.account = account;
	}
	public Json_Account getAccount(){
		return this.account;
	}
	
	final Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				LogUtil.d(TAG, "" + msg.what);
				if(dataReady!=null)
					dataReady.setData(msg.obj);
				break;
			case 1:
				LogUtil.d(TAG, "" + msg.what);
//				dataReady.setData(msg.obj);
				break;
			}
		}
	};

	public void login(final String name, final String pswd,
			final OnDataReady dataReady) {
		this.dataReady = dataReady;

		LoginCommand logincmd = new LoginCommand(name, pswd);
		executeCmd(logincmd);
	}

	//*********************************************************************************************

	class LoginCommand implements Command {
		String user, password;

		public LoginCommand(String user, String password) {
			this.user = user;
			this.password = password;
		}

		@Override
		public void execute() {
			try {
				Object obj = util.login(user, password);
//				if (dataReady != null)
//					dataReady.setData(obj);
				if(handler!=null)
					handler.sendMessage(Message.Obtain(handler, 0, obj));
			} catch (Exception e) {
				e.printStackTrace();
				if(handler!=null)
					handler.sendMessage(Message.Obtain(handler, 1, null));
			}
		}
	}

	//*********************************************************************************************

	public void getMyCompany(final Json_Account account, final OnDataReady dataReady) {
		this.dataReady = dataReady;

		GetMyCompanyCommand getmycompanycmd = new GetMyCompanyCommand(account);
		executeCmd(getmycompanycmd);
	}

	class GetMyCompanyCommand implements Command {
		public GetMyCompanyCommand(Json_Account account) {
			setAccount(account);
		}
		@Override
		public void execute() {
			if(getAccount()==null)
				return;
			try {
				Object obj = util.getMyCompany(getAccount());

				if(handler!=null)
					handler.sendMessage(Message.Obtain(handler, 0, obj));
				
			} catch (Exception e) {
				e.printStackTrace();
				if(handler!=null)
					handler.sendMessage(Message.Obtain(handler, 1, null));
			}
		}		
	}
	class LogOutCommand implements Command {
		public LogOutCommand() {
		}

		@Override
		public void execute() {
			
		}
	}
}